from flask import Flask, request, jsonify,make_response
from settings import Sttings
from flask_sqlalchemy import SQLAlchemy
from flask_cors import CORS
import json,time

app = Flask(__name__)
# 跨域
CORS(app)
app.config.from_object(Sttings)
bf = SQLAlchemy(app)


# orm创表********************************
class Order(bf.Model):
    __tablename__ = "order"
    id = bf.Column(bf.Integer, primary_key=True, autoincrement=True)
    name = bf.Column(bf.String(30), unique=True)
    price = bf.Column(bf.Float)



# ********************
# 创建表
# bf.create_all()


@app.route('/shops', methods=['GET', 'POST','DELETE'])
def add():
    if request.method == "POST":
        data = json.loads(request.data)
        shop = Order(name=data['name'], price=data['price'])
        bf.session.add(shop)
        bf.session.commit()
        return jsonify({"code": 200})
    elif request.method == "GET":
        list = []
        order = Order.query.all()
        for i in order:
            dict = {}
            dict['name'] = i.name
            dict['id'] = i.id
            dict['price'] = int(int(i.price)*0.8)
            list.append(dict)
        return {"code":200,"list":list}
    elif  request.method == "DELETE":
        id = request.args.get('id')
        Order.query.filter_by(id=id).delete()
        bf.session.commit()
        return 'ok'

if __name__ == '__main__':
    app.run()
